package com.coolwalker.common.domain;

import java.io.Serializable;
import java.util.Map;

/**
 * 返回结果
 *
 * @author xuebing
 * @date 2019-08-05
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 8574754291570429887L;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 错误码
     */
    private int errorCode;

    /**
     * 错误信息
     */
    private String errorMessage;

    /**
     * 内部信息
     */
    private String innerMessage;

    /**
     * 扩展字段
     */
    private Map<String, Object> extensions;

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.TRUE);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> error(Integer errorCode, String errorMessage, String innerMessage) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.FALSE);
        result.setErrorCode(errorCode);
        result.setInnerMessage(innerMessage);
        result.setErrorMessage(errorMessage);
        return result;
    }

    public static <T> Result<T> error(Integer errorCode, String errorMessage) {
        return error(errorCode, errorMessage, null);
    }

    public static <T> Result<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMessage(), null);
    }

    public static <T> Result<T> error(ErrorCode errorCode, String innerMessage) {
        return error(errorCode.getCode(), errorCode.getMessage(), innerMessage);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getInnerMessage() {
        return innerMessage;
    }

    public void setInnerMessage(String innerMessage) {
        this.innerMessage = innerMessage;
    }

    public Map<String, Object> getExtensions() {
        return extensions;
    }

    public void setExtensions(Map<String, Object> extensions) {
        this.extensions = extensions;
    }
}
